package hc.uco

class EvolucaoController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [evolucaoInstanceList: Evolucao.list(params), evolucaoInstanceTotal: Evolucao.count()]
    }

    def create = {
        def evolucaoInstance = new Evolucao()
        evolucaoInstance.properties = params
		def paciente = Paciente.get(evolucaoInstance.paciente.id)
		params['paciente.id'] = evolucaoInstance.paciente.id
		def evoList = Evolucao.findByPaciente(paciente)
		if (evoList==null) {
			evolucaoInstance.diaNumero = 1
			evolucaoInstance.diaData = paciente.dataInclusao
		} else {
			def lastEvo = evoList.get(evoList.count())
			evolucaoInstance.diaNumero = lastEvo.diaNumero + 1
			evolucaoInstance.diaData = lastEvo.diaData + 1
		}
        return [evolucaoInstance: evolucaoInstance, 'pacienteInstance':paciente]
    }

    def save = {
        def evolucaoInstance = new Evolucao(params)
		evolucaoInstance.createdBy = session.usuario;
		evolucaoInstance.updatedBy = evolucaoInstance.createdBy;
        if (evolucaoInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'evolucao.label', default: 'Evolucao'), evolucaoInstance.id])}"
            //redirect(action: "show", id: evolucaoInstance.id)
			redirect(action: "manage", id: evolucaoInstance.paciente.id)
        }
        else {
            render(view: "create", model: [evolucaoInstance: evolucaoInstance, 'pacienteInstance':evolucaoInstance.paciente])
        }
    }

    def show = {
        def evolucaoInstance = Evolucao.get(params.id)
        if (!evolucaoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'evolucao.label', default: 'Evolucao'), params.id])}"
            redirect(action: "list")
        }
        else {
            [evolucaoInstance: evolucaoInstance]
        }
    }

    def edit = {
        def evolucaoInstance = Evolucao.get(params.id)
        if (!evolucaoInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'evolucao.label', default: 'Evolucao'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [evolucaoInstance: evolucaoInstance]
        }
    }

    def update = {
        def evolucaoInstance = Evolucao.get(params.id)
        if (evolucaoInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (evolucaoInstance.version > version) {
                    
                    evolucaoInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'evolucao.label', default: 'Evolucao')] as Object[], "Another user has updated this Evolucao while you were editing")
                    render(view: "edit", model: [evolucaoInstance: evolucaoInstance])
                    return
                }
            }
            evolucaoInstance.properties = params
            if (!evolucaoInstance.hasErrors() && evolucaoInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'evolucao.label', default: 'Evolucao'), evolucaoInstance.id])}"
                redirect(action: "show", id: evolucaoInstance.id)
            }
            else {
                render(view: "edit", model: [evolucaoInstance: evolucaoInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'evolucao.label', default: 'Evolucao'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def evolucaoInstance = Evolucao.get(params.id)
        if (evolucaoInstance) {
            try {
                evolucaoInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'evolucao.label', default: 'Evolucao'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'evolucao.label', default: 'Evolucao'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'evolucao.label', default: 'Evolucao'), params.id])}"
            redirect(action: "list")
        }
    }
	
	def manage = {
		
		def evolucao = new Evolucao()
		evolucao.properties = params
		
		if (request.method == 'GET') {
			def paciente = Paciente.get(params.id)
			params['paciente.id'] = params.id
			params.sort = "diaNumero"
			params.order = "asc"
			def evolucaoInstanceList = Evolucao.findAll("from Evolucao as e where e.paciente=:pac order by e.diaNumero asc", [pac:paciente])
			return ['pacienteInstance':paciente, 'evolucaoInstanceList': evolucaoInstanceList ]
		} else {
			evolucao.save()
		}
	}
}
